﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Diagnostics;
using IOPath = System.IO.Path;
using Microsoft.Office.OneNote.Interop;

namespace Microsoft.Office.OneNote
{
    [DebuggerDisplay("Notebook {Nickname}")]
    [OneNoteXmlElementAttribute("Notebook")]
    [OneNoteHierarchyType(CreateHierarchyType.Notebook)]
    public sealed class OneNoteNotebook : OneNoteFolder
    {
        internal static readonly OneNoteProperty ColorProperty = OneNoteProperty.Register("color", typeof(OneNoteColor), typeof(OneNoteNotebook), OneNoteColor.Automatic);
        internal static readonly OneNoteProperty NicknameProperty = OneNoteProperty.Register("nickname", typeof(String), typeof(OneNoteNotebook));

        private OneNoteObjectCollection<OneNoteSectionBase> _sections;

        internal OneNoteNotebook(OneNoteObjectId id)
            : base (id)
        {
        }

        public OneNoteColor Color
        {
            get { return GetValue<OneNoteColor>(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        public String Nickname
        {
            get { return GetValue<String>(NicknameProperty); }
            set { SetValue(NicknameProperty, value); }
        }

        public OneNoteObjectCollection<OneNoteSectionBase> Sections
        {
            get
            {
                if (_sections == null)
                {
                    EnsureStorageInitialized();
                    _sections = new OneNoteObjectCollection<OneNoteSectionBase>(Storage);
                }

                return _sections;
            }
        }

        public OneNoteSection OpenSection(string name)
        {
            return OpenSection(name, false);
        }

        public OneNoteSection OpenSection(string name, bool openExisting)
        {
            return OneNoteSection.Open(name, this, openExisting);
        }

        public OneNoteSectionGroup OpenSectionGroup(string name)
        {
            return OpenSectionGroup(name, false);
        }

        public OneNoteSectionGroup OpenSectionGroup(string name, bool openExisting)
        {
            Contract.RequiresValidFileName(name, "name");

            return Storage.Open<OneNoteSectionGroup>(name, ObjectId, openExisting);
        }

        internal override HierarchyScope UpdateScope
        {
            get { return HierarchyScope.Sections; }
        }

        internal OneNoteSection GetSectionByPath(string path)
        {
            if (IsAncestorOf(path))
            {
                return GetByPathIn<OneNoteSection>(Sections, path);
            }

            return null;
        }

        private T GetByPathIn<T>(IEnumerable<OneNoteSectionBase> sections, string path)
            where T : OneNoteSectionBase
        {
            foreach (OneNoteSectionBase s in sections)
            {
                if (s.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
                {
                    return (s as T);
                }
                else if (s.IsAncestorOf(path))
                {
                    return GetByPathIn<T>(s.Sections, path);
                }
            }

            return null;
        }
    }
}
